'use strict';
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
const A = require('../nodes/identity.cjs'),
  K = require('../nodes/Pair.cjs'),
  P = require('../nodes/YAMLMap.cjs'),
  U = require('../nodes/YAMLSeq.cjs'),
  $ = require('./resolve-end.cjs'),
  S = require('./resolve-props.cjs'),
  R = require('./util-contains-newline.cjs'),
  Y = require('./util-map-includes.cjs'),
  y = 'Block collections are not allowed within flow collections',
  T = d => d && (d.type === 'block-map' || d.type === 'block-seq');
function B({ composeNode: d, composeEmptyNode: _ }, n, p, t, b) {
  const r = p.start.source === '{',
    a = r ? 'flow map' : 'flow sequence',
    O = (b == null ? void 0 : b.nodeClass) ?? (r ? P.YAMLMap : U.YAMLSeq),
    i = new O(n.schema);
  i.flow = !0;
  const k = n.atRoot;
  k && (n.atRoot = !1);
  let w = p.offset + p.start.source.length;
  for (let o = 0; o < p.items.length; ++o) {
    const N = p.items[o],
      { start: q, key: g, sep: f, value: l } = N,
      e = S.resolveProps(q, {
        flow: a,
        indicator: 'explicit-key-ind',
        next: g ?? (f == null ? void 0 : f[0]),
        offset: w,
        onError: t,
        startOnNewline: !1,
      });
    if (!e.found) {
      if (!e.anchor && !e.tag && !f && !l) {
        o === 0 && e.comma
          ? t(e.comma, 'UNEXPECTED_TOKEN', `Unexpected , in ${a}`)
          : o < p.items.length - 1 &&
            t(e.start, 'UNEXPECTED_TOKEN', `Unexpected empty item in ${a}`),
          e.comment &&
            (i.comment
              ? (i.comment +=
                  `
` + e.comment)
              : (i.comment = e.comment)),
          (w = e.end);
        continue;
      }
      !r &&
        n.options.strict &&
        R.containsNewline(g) &&
        t(
          g,
          'MULTILINE_IMPLICIT_KEY',
          'Implicit keys of flow sequence pairs need to be on a single line',
        );
    }
    if (o === 0)
      e.comma && t(e.comma, 'UNEXPECTED_TOKEN', `Unexpected , in ${a}`);
    else if (
      (e.comma || t(e.start, 'MISSING_CHAR', `Missing , between ${a} items`),
      e.comment)
    ) {
      let m = '';
      e: for (const s of q)
        switch (s.type) {
          case 'comma':
          case 'space':
            break;
          case 'comment':
            m = s.source.substring(1);
            break e;
          default:
            break e;
        }
      if (m) {
        let s = i.items[i.items.length - 1];
        A.isPair(s) && (s = s.value ?? s.key),
          s.comment
            ? (s.comment +=
                `
` + m)
            : (s.comment = m),
          (e.comment = e.comment.substring(m.length + 1));
      }
    }
    if (!r && !f && !e.found) {
      const m = l ? d(n, l, e, t) : _(n, e.end, f, null, e, t);
      i.items.push(m), (w = m.range[2]), T(l) && t(m.range, 'BLOCK_IN_FLOW', y);
    } else {
      const m = e.end,
        s = g ? d(n, g, e, t) : _(n, m, q, null, e, t);
      T(g) && t(s.range, 'BLOCK_IN_FLOW', y);
      const c = S.resolveProps(f ?? [], {
        flow: a,
        indicator: 'map-value-ind',
        next: l,
        offset: s.range[2],
        onError: t,
        startOnNewline: !1,
      });
      if (c.found) {
        if (!r && !e.found && n.options.strict) {
          if (f)
            for (const u of f) {
              if (u === c.found) break;
              if (u.type === 'newline') {
                t(
                  u,
                  'MULTILINE_IMPLICIT_KEY',
                  'Implicit keys of flow sequence pairs need to be on a single line',
                );
                break;
              }
            }
          e.start < c.found.offset - 1024 &&
            t(
              c.found,
              'KEY_OVER_1024_CHARS',
              'The : indicator must be at most 1024 chars after the start of an implicit flow sequence key',
            );
        }
      } else
        l &&
          ('source' in l && l.source && l.source[0] === ':'
            ? t(l, 'MISSING_CHAR', `Missing space after : in ${a}`)
            : t(c.start, 'MISSING_CHAR', `Missing , or : between ${a} items`));
      const h = l ? d(n, l, c, t) : c.found ? _(n, c.end, f, null, c, t) : null;
      h
        ? T(l) && t(h.range, 'BLOCK_IN_FLOW', y)
        : c.comment &&
          (s.comment
            ? (s.comment +=
                `
` + c.comment)
            : (s.comment = c.comment));
      const v = new K.Pair(s, h);
      if ((n.options.keepSourceTokens && (v.srcToken = N), r)) {
        const u = i;
        Y.mapIncludes(n, u.items, s) &&
          t(m, 'DUPLICATE_KEY', 'Map keys must be unique'),
          u.items.push(v);
      } else {
        const u = new P.YAMLMap(n.schema);
        (u.flow = !0), u.items.push(v), i.items.push(u);
      }
      w = h ? h.range[2] : c.end;
    }
  }
  const C = r ? '}' : ']',
    [I, ...L] = p.end;
  let M = w;
  if (I && I.source === C) M = I.offset + I.source.length;
  else {
    const o = a[0].toUpperCase() + a.substring(1),
      N = k
        ? `${o} must end with a ${C}`
        : `${o} in block collection must be sufficiently indented and end with a ${C}`;
    t(w, k ? 'MISSING_CHAR' : 'BAD_INDENT', N),
      I && I.source.length !== 1 && L.unshift(I);
  }
  if (L.length > 0) {
    const o = $.resolveEnd(L, M, n.options.strict, t);
    o.comment &&
      (i.comment
        ? (i.comment +=
            `
` + o.comment)
        : (i.comment = o.comment)),
      (i.range = [p.offset, M, o.offset]);
  } else i.range = [p.offset, M, M];
  return i;
}
exports.resolveFlowCollection = B;
